// Funnelback auto-completion jQuery plugin // Author: Nicolas Guillaumin, Matt Sheppard // Copyright Funnelback, 2012 // $Id: jquery.funnelback-completion.js 41163 2014-11-28 06:00:43Z msheppard $ (function($) { $.fn.fbcompletion = function(settings) { var facetBasedCompletionSuccess = function(config, request, data) { var responses = new Array(); var partial_query_parts = request.term.split(" "); var last_partial_term = partial_query_parts[partial_query_parts.length -1]; var partial_complete_query = request.term.substring(0, request.term.length - last_partial_term.length); if(last_partial_term.substring(0,1) == '|') { last_partial_term = last_partial_term.substring(last_partial_term.indexOf(':') + 1, last_partial_term.length); } var lower_last_partial_term = last_partial_term.toLowerCase(); var facets = data.response.facets; var rank = 1; for (var i=0; i 0) ? "#fb-queryform" : "body", source: function (request, response) { var finished = [!config.searchBasedCompletionEnabled, !config.standardCompletionEnabled]; var allResponses = [new Array(), new Array()]; if(config.searchBasedCompletionEnabled) { jQuery.ajax({ type: 'GET', url: config.searchBasedCompletionProgram + '?' + replaceQueryInQuestionWithPartialQuery(config.query, request.term, config.searchBasedCompletionProfile), dataType: 'jsonp', error: function(xhr, textStatus, errorThrown) { if (window.console) { console.log('Autocomplete error: ' + textStatus + ', ' + errorThrown); } }, success: function(data) { //Call the function that will process the json. allResponses[0] = config.searchBasedCompletionFunction(config, request, data); finished[0] = true; if(finished[1]) { response (allResponses[1].concat(allResponses[0])); } } }); } if(config.standardCompletionEnabled) { jQuery.ajax({ type: 'GET', url: config.program + '?collection=' + config.collection + '&partial_query=' + request.term.replace(/ /g, '+') + '&show=' + config.show + '&sort=' + config.sort + '&alpha=' + config.alpha + '&fmt=' + ((config.format == 'simple') ? 'json' : 'json++') + ((config.profile !== '') ? '&profile=' + config.profile : '' ) , dataType: 'jsonp', error: function(xhr, textStatus, errorThrown) { if (window.console) { console.log('Autocomplete error: ' + textStatus + ', ' + errorThrown); } }, success: function(data) { var responses = allResponses[1]; for (var i=0; i

').parent().html(); } else { label = '[Error: jQuery template plugin is unavailable]'; } break; case 'C': // JS callback label = eval(item.extra.disp); break; case 'T': // Plain text label = item.label.replace(new RegExp('('+item.matchOn+')', 'i'), '$1'); break; case 'H': // HTML // Label cannot be highlighted as there's no way // to skip HTML tags when running the regexp, possibly // corrupting them (ex: '+item.category+""); currentCategory = item.category; } that._renderItemData(ul, item); }); } }); return this; }; /** * Replaces the query= value with the completed part of the partial query * *

e.g. if question is query=old&bar=foo and partialQuery is 'hello moth' *it will return query=hello&bar=foo

* * */ function replaceQueryInQuestionWithPartialQuery(encodedQuestion, partialQuery, profile) { var question = htmlDecode(encodedQuestion); var partial_query_parts = partialQuery.split(" "); var last_partial_term = partial_query_parts[partial_query_parts.length -1]; var partial_complete_query = partialQuery.substring(0, partialQuery.length - last_partial_term.length); if(partial_complete_query.length == 0) { partial_complete_query = '!padrenull'; } var questionsParts = question.split('&'); var newQuestion = ''; var changedQuery = false; var changedProfile = false; for(var i = 0; i < questionsParts.length; i++) { if(questionsParts[i].substring(0, 6) == 'query=') { newQuestion += 'query=' + partial_complete_query; changedQuery = true; } else if(profile != null && questionsParts[i].substring(0, 8) == 'profile=') { newQuestion += 'profile=' + profile; changedProfile = true; } else { newQuestion += questionsParts[i]; } newQuestion += '&'; } if(!changedQuery) { newQuestion += 'query=' + partial_complete_query; newQuestion += '&'; } if(!changedProfile && profile != null) { newQuestion += 'profile=' + profile; newQuestion += '&'; } return newQuestion.substring(0, newQuestion.length -1); } function htmlDecode(value){ return $('
').html(value).text(); } })(jQuery);